//
//  CZMessageCell.m
//  QQ
//
//  Created by MAC on 16-3-10.
//  Copyright (c) 2016年 MAC. All rights reserved.
//

#import "CZMessageCell.h"
#import "CZMessage.h"
#import "CZMessageFrame.h"
@interface CZMessageCell()

@property(nonatomic,weak)UILabel* lblTime;
@property(nonatomic,weak)UIImageView *imgViewIcon;
@property(nonatomic,weak)UIButton * btnText;
@end


@implementation CZMessageCell

//重写该initWithStyle初始化方法
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        
        //创建子控件
        //1.显示时间的label
        
        UILabel *lblTime = [[UILabel alloc]init];
        
        //设置文字大小
        lblTime.font = [UIFont systemFontOfSize:12];
        //设置文字居中
        lblTime.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:lblTime];
        self.lblTime = lblTime;
        
        //2.显示头像的uiimage
        UIImageView *imgViewIcon = [[UIImageView alloc]init];
        [self.contentView addSubview:imgViewIcon];
        self.imgViewIcon = imgViewIcon;
        
        
        //3.显示正文的按钮
        UIButton *btnText = [[UIButton alloc]init];
        //设置正文的文字大小
        btnText.titleLabel.font = textFont;
        //设置文字颜色
        [btnText setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        //正文文字换行
        btnText.titleLabel.numberOfLines = 0;
        //设置按钮的背景色
        //btnText.backgroundColor = [UIColor purpleColor];
        
        //设置按钮中的laberl背景色
        //btnText.titleLabel.backgroundColor = [UIColor greenColor];
        //设置按钮的内边距

        btnText.contentEdgeInsets = UIEdgeInsetsMake(15, 20, 15, 20);
        
        [self.contentView addSubview:btnText];
        self.btnText = btnText;
    }
    
    self.backgroundColor = [UIColor clearColor];
    return self;
}


//重写frame模型的set方法
-(void)setMessageFrame:(CZMessageFrame *)messageFrame
{
    _messageFrame = messageFrame;
    
    //获取数据模型
    CZMessage*message = messageFrame.message;
    //分别设置每个子控件的数据和frame
    //1.设置时间label的数据和frame
    self.lblTime.text = message.time;
    self.lblTime.frame = messageFrame.timeFrame;
    //2.设置头像的数据和frame
    NSString* iconImg = message.type == CZMessageTypeMe?@"me":@"other";
    
    self.imgViewIcon.image = [UIImage imageNamed:iconImg];
    self.imgViewIcon.frame = messageFrame.iconFrame;
    
    //3.设置正文数据和frame
    [self.btnText setTitle:message.text forState:UIControlStateNormal];
    self.btnText.frame = messageFrame.textFrame;
    
    //4.设置正文背景
    NSString* imgNor, *imgHighLighted;
    if(message.type == CZMessageTypeMe)
    {
        imgNor = @"chat_send_nor";
        imgHighLighted = @"chat_send_press_pic";
        //自己发的消息，设置颜色为白色
        [self.btnText setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }else{
        imgNor = @"chat_recive_nor";
        imgHighLighted = @"chat_recive_press_pic";
        [self.btnText setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    //加载图片
    UIImage* imageNormal = [UIImage imageNamed:imgNor];
    UIImage* imageHighLighted = [UIImage imageNamed:imgHighLighted];
    
    //用平铺的方式拉伸图片
    imageNormal = [imageNormal stretchableImageWithLeftCapWidth:imageNormal.size.width*0.5 topCapHeight:imageNormal.size.height*0.5];
    
    imageHighLighted = [imageHighLighted stretchableImageWithLeftCapWidth:imageHighLighted.size.width*0.5 topCapHeight:imageHighLighted.size.height*0.5];
    
    //设置
    [self.btnText setBackgroundImage:imageNormal forState:UIControlStateNormal];
    [self.btnText setBackgroundImage:imageHighLighted forState:UIControlStateHighlighted];
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
